package com.hxd.otc.module.wallet.model;

import android.support.annotation.NonNull;

import com.google.gson.Gson;
import com.hxd.otc.api.IApi;
import com.hxd.otc.api.IWalletApi;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.module.my.bean.WalletContactsBean;
import com.hxd.otc.module.wallet.bean.CoinOneKeyRateVoBean;
import com.hxd.otc.module.wallet.bean.OneKeyCoinTypeRequest;
import com.hxd.otc.module.wallet.bean.TransferRecoderBean;
import com.hxd.otc.module.wallet.bean.WalletAccountInfoBean;
import com.hxd.otc.module.wallet.bean.WalletCoinRateVoBean;
import com.hxd.otc.module.wallet.bean.WalletCoinRecordBean;
import com.hxd.otc.module.wallet.bean.WalletCurrencyBean;
import com.hxd.otc.module.wallet.bean.WalletFeeRuleBean;
import com.hxd.otc.module.wallet.bean.WalletOrderInfoBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.hxd.otc.utils.EncryptUtils;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 包  名 :  com.hxd.otc.mvp.wallet
 * 类  名 :  WalletModel
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/6 14:54
 * 描  述 :  ${TODO}
 */
public class WalletModel {

    private static class SingletonHolder {
        private static final WalletModel instance = new WalletModel();
    }

    public static WalletModel getInstance() {
        return WalletModel.SingletonHolder.instance;
    }


    /**
     * 获取验证码
     *
     * @param
     * @param
     */
    public void getCode(BaseObserver observer,
                        AutoDisposeConverter<BaseBean> converter, String phoneHost, String phoneNumber, String type) {
    }


    /**
     * 获取钱包信息，余额
     *
     * @param observer
     * @param converter
     */
    public void getWalletAccountInfo(BaseObserver observer, AutoDisposeConverter<BaseBean<WalletAccountInfoBean>> converter, String legalTenderType) {
        getIWalletApi()
                .getWalletAccountInfo(legalTenderType)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);

    }


    /**
     * 获取OTC账户钱包信息
     *
     * @param observer
     * @param converter
     */
    public void getWalletAccountInfo(BaseObserver observer, AutoDisposeConverter<BaseBean<List<WalletCurrencyBean>>> converter, boolean isNorInfo) {

        if (isNorInfo) {
            getIWalletApi()
                    .getNormalAccountInfo()
                    .observeOn(AndroidSchedulers.mainThread())
                    .as(converter)
                    .subscribe(observer);
        } else {
            getIWalletApi()
                    .getOtcAccountInfo()
                    .observeOn(AndroidSchedulers.mainThread())
                    .as(converter)
                    .subscribe(observer);
        }

    }


    public void coinWithdraw(BaseObserver observer, AutoDisposeConverter<BaseBean> converter,
                             String[] paramters) {
        getIWalletApi()
                .getWithdrawInfo(paramters[1], paramters[2], paramters[3])
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void postGooleValidate(BaseObserver observer, AutoDisposeConverter<BaseBean> converter, String id, String googleCode) {
        getIWalletApi()
                .postGooleValidate(id, googleCode)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    private IWalletApi getIWalletApi() {
        return RetrofitManager.getDefault()
                .create(IWalletApi.class);
    }

    /**
     * @param observer
     * @param converter
     * @param amount      数量
     * @param captchCode  验证码 ,
     * @param coinType    币种
     * @param fromAddress 源钱包地址
     * @param fundPasswd  交易密码
     * @param orderId     (string, optional): 订单号:普通交易不需要填写，如果是商家扫描订单，需要添加 ,
     * @param payType     支付方式：0 AEC转账(AEC付款) 1 AEC商家支付 2 MSC商家支付 ,
     * @param toAddress   目标钱包地址
     */
    public void coinTransfer(BaseObserver observer, AutoDisposeConverter<BaseBean> converter,
                             String amount, String captchCode, String coinType,
                             String fromAddress, String fundPasswd, String orderId, String payType, String toAddress) {
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();
        map.put("amount", amount);
        map.put("captchCode", captchCode);
        map.put("coinType", coinType);
        map.put("fromAddress", fromAddress);
        map.put("fundPasswd", EncryptUtils.getRSA(fundPasswd));
        map.put("orderId", orderId);
        map.put("payType", payType);
        map.put("toAddress", toAddress);

        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE), gson.toJson(map));
        getIWalletApi()
                .coinTransfer(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    public void getWalletCoinRecord(BaseObserver observer, AutoDisposeConverter<BaseBean<WalletCoinRecordBean>> converter, long page, long size) {
        getIWalletApi()
                .getWalletCoinRecord(page, size)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    /**
     * 获取汇率
     *
     * @param observer
     * @param converter
     */
    public void getWalletCoinRate(BaseObserver<List<WalletCoinRateVoBean>> observer, AutoDisposeConverter<BaseBean<List<WalletCoinRateVoBean>>> converter) {
        getIWalletApi()
                .getWalletCoinRate()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);

    }

    /**
     * 获取汇率
     *
     * @param observer
     * @param converter
     */
    public void getCoinChangeThanRate(BaseObserver<List<CoinOneKeyRateVoBean>> observer, AutoDisposeConverter<BaseBean<List<CoinOneKeyRateVoBean>>> converter) {
        getIWalletApi()
                .getCoinChangeThanRate()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);

    }

    /**
     * 一键换币接口
     *
     * @param observer
     * @param converter
     * @param coinType
     * @param payPassword
     * @param tradeCoinDtoList
     */
    public void oneCoinTypeChange(BaseObserver observer, AutoDisposeConverter<BaseBean> converter,
                                  String coinType, String payPassword, List<OneKeyCoinTypeRequest> tradeCoinDtoList) {
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        map.put("coinType", coinType);
        map.put("tradeCoinDtoList", tradeCoinDtoList);
        map.put("payPassword", EncryptUtils.getRSA(payPassword));

        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE), gson.toJson(map));
        getIWalletApi()
                .oneCoinTypeChange(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * AEC 转 MSC
     *
     * @param observer
     * @param converter
     * @param aecAddress aec钱包地址
     * @param aecAmount  aec数量
     * @param captchCode 验证码
     * @param fundPasswd 交易密码
     * @param mscAddress msc钱包地址
     * @param mscAmount  msc数量
     */
    public void AECChangeMSC(BaseObserver observer, AutoDisposeConverter<BaseBean> converter,
                             String aecAddress, String aecAmount, String captchCode, String fundPasswd, String mscAddress, String mscAmount) {
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();
        map.put("aecAddress", aecAddress);
        map.put("aecAmount", aecAmount);
        map.put("captchCode", captchCode);
        map.put("payPassword", EncryptUtils.getRSA(fundPasswd));
        map.put("mscAddress", mscAddress);
        map.put("mscAmount", mscAmount);

        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE), gson.toJson(map));
        getIWalletApi()
                .AECChangeMSC(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    /**
     * 其他币 转 AEC
     *
     * @param observer
     * @param converter
     * @param aec2btcAmount BTC折合AEC数量
     * @param aec2ethAmount ETH折合AEC数量
     * @param aec2ltcAmount LTC折合AEC数量
     * @param aecAddress    aec钱包地址
     * @param btcAddress    btc钱包地址
     * @param btcAmount     btc数量
     * @param captchCode    验证码
     * @param fundPasswd    交易密码
     * @param ltcAddress    ltc钱包地址
     * @param ltcAmount     ltc数量
     */
    public void CoinChangeAEC(BaseObserver observer, AutoDisposeConverter<BaseBean> converter,
                              String aec2btcAmount, String aec2ltcAmount, String aec2ethAmount,
                              String aecAddress,
                              String btcAddress, String btcAmount,
                              String captchCode, String fundPasswd,
                              String ltcAddress, String ltcAmount,
                              String ethAddress, String ethAmount) {
        Gson gson = new Gson();
        Map<String, String> map = new HashMap<>();
        map.put("aec2btcAmount", aec2btcAmount);
        map.put("aec2ltcAmount", aec2ltcAmount);
        map.put("aec2ethAmount", aec2ethAmount);
        map.put("aecAddress", aecAddress);
        map.put("btcAddress", btcAddress);
        map.put("btcAmount", btcAmount);
        map.put("captchCode", captchCode);
        map.put("ethAddress", ethAddress);
        map.put("ethAmount", ethAmount);
        map.put("fundPasswd", EncryptUtils.getRSA(fundPasswd));
        map.put("ltcAddress", ltcAddress);
        map.put("ltcAmount", ltcAmount);
        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE), gson.toJson(map));
        getIWalletApi()
                .CoinChangeAEC(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getContactByUserId(BaseObserver<ArrayList<WalletContactsBean>> observer,
                                   AutoDisposeConverter<BaseBean<ArrayList<WalletContactsBean>>> converter,
                                   String id) {
        getIWalletApi()
                .getContactByUserId(id)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void deleteContactById(String id, BaseObserver observer, AutoDisposeConverter<BaseBean> converter) {
        getIWalletApi()
                .deleteContactById(id)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void addWalletContact(
            BaseObserver observer, AutoDisposeConverter<BaseBean> converter, String... paramters) {
        getIWalletApi()
                .addWalletContact(paramters[1], paramters[2], paramters[3])
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    /**
     * 提币手续费规则信息
     */
    public void bringCoinFeeRule(@NonNull BaseObserver<ArrayList<WalletFeeRuleBean>> observer, @NonNull AutoDisposeConverter<BaseBean<ArrayList<WalletFeeRuleBean>>> converter) {
        getIWalletApi()
                .bringCoinFeeRule()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getOrderInfo(String id, BaseObserver<WalletOrderInfoBean> observer, AutoDisposeConverter<BaseBean<WalletOrderInfoBean>> converter) {
        getIWalletApi()
                .getOrderInfo(id)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void legalTendeDeal(BaseObserver<List<WalletCoinRateVoBean>> observer, AutoDisposeConverter<BaseBean<List<WalletCoinRateVoBean>>> converter) {
        RetrofitManager.getDefault().create(IWalletApi.class)
                .legalTendeDeal()
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    public void otcToNormalAccount(BaseObserver observer, AutoDisposeConverter<BaseBean> converter, String... paramters) {
        RetrofitManager.getDefault().create(IWalletApi.class)
                .normalToOtcAccount(paramters[1], paramters[2], paramters[3], paramters[4])
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);


    }

    public void getRechargeCoinAddress(BaseObserver observer, AutoDisposeConverter<BaseBean> converter, String... paramters) {
        RetrofitManager.getDefault().create(IWalletApi.class)
                .getRechargeCoinAddress(paramters[0])
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);


    }

    public void getTransferRecoder(BaseObserver observer, AutoDisposeConverter<BaseBean<TransferRecoderBean>> converter, long page, long size) {
        RetrofitManager.getDefault().create(IWalletApi.class)
                .getTransferRecoder(page, size)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void getAssetBySymbol(BaseObserver observer, AutoDisposeConverter<BaseBean> converter, String symbol) {
        RetrofitManager.getDefault().create(IWalletApi.class)
                .getAssetBySymbol(symbol)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }
}
